<?php
namespace app\api\controller\v1;
use app\admin\model\Area;
use app\common\model\LitestoreAdress;
use app\common\model\LitestoreShop;
use app\common\service\LitestoreAddress;
use app\common\service\LitestoreOrderAddress as LitestoreOrderAddressService;

class Store extends Base{
    protected $noNeedRight = '*';
    /**
     * 商家收货地址
     */
 public function storeAddress(){
       $userinfo = $this->userinfo;
       $LitestoreShop = LitestoreShop::get(1);
       $address = $LitestoreShop->getAddressbyUser()->field('id,name,phone,detail,order_id,user_id,createtime')->select();
     if(!$address){
         $this->error('暂没有收货地址');
     }
     $this->success("请求成功",$address);
}

    /**
     * 已这个获取商家地址
     */
    public function getAddress(){
        $AreaList=new Area();
       try{
           $userinfo = $this->auth->getUser();
           $uid=$userinfo['id'];

           $LitestoreShop = new LitestoreAdress();
           $address = $LitestoreShop
               ->where(['user_id'=>$uid])
               ->field('address_id,name,phone,detail,user_id,province_id,city_id,region_id,isdefault,createtime')
               ->find();

           if(!$address){
               $this->error('暂没有收货地址');
           }

           $address=$address->toArray();
           $provinceOne=$AreaList->where(['id'=>$address['province_id']])->find();
           $cityOne=$AreaList->where(['id'=>$address['city_id']])->find();
           $areaOne=$AreaList->where(['id'=>$address['region_id']])->find();

           if(!empty($provinceOne)){
               $address['province_name']=$provinceOne->name;
           }
           if(!empty($cityOne)){
               $address['city_name']=$cityOne->name;
           }
           if(!empty($areaOne)){
               $address['area_name']=$areaOne->name;
           }


       }catch (\Exception $e){
           $this->error('获取地址错误！');
       }
        $this->success("请求成功",$address);
    }
    /**
     * 添加收货地址
     */
public function createAddress(){
    $post = $this->request->request('rows');
    $userinfo = $this->auth->getUser();
    $uid=$userinfo['id'];
    if(!$post){
        $datas = json_encode((object)null);
        $this->error("传参格式有误",$datas,0);
    }
    $data = json_decode($post,true);

    if(empty($data['detail'])){
    $datas = json_encode((object)null);
    $this->error("收货地址不能为空",$datas,0);
}
//    $LitestoreOrderAddressService = new LitestoreOrderAddressService();
    $LitestoreOrderAddressService = new LitestoreAddress();
    $data['user_id']=$uid;
    $map['user_id'] = $uid;
    $res = $LitestoreOrderAddressService->createAddress($map,$data);

    if(!$res){
        $this->error($res['msg'],$res['data'],$res['code']);
    }
    $this->success($res['msg'],$res['data'],$res['code']);
}
    /**
     * 编辑收货地址
     */
    public function edite(){
        $post = $this->request->request('rows');
        if(!$post){
            $datas = json_encode((object)null);
            $this->error("传参格式有误",$datas,0);
        }
        $data = json_decode($post,true);
        $data['isdefault']=1;
    $LitestoreAddressModel = new LitestoreAdress();
    $map['address_id']= array('eq',$data['address_id']);
    $res = $LitestoreAddressModel->save($data,$map);

    if(!$res){
        $this->error('编辑收货地址失败');
    }
    $this->success("编辑收货地址成功");
}
    /**
     * 删除收货地址
     */
    public function delete(){
        $id = $this->request->request('id');
        $LitestoreOrderAddressService = new LitestoreOrderAddressService();
        $map= ['id'=>$id];
        $res = $LitestoreOrderAddressService->delete($map);
        if(!$res){
            $this->error('删除收货地址失败');
        }
        $this->success("删除收货地址成功");
    }
    /**
     * 用户的二维码
     */
    public function getMyinCode(){
        $userinfo = $this->userinfo;
        $code = $userinfo['code'];
        if(!$code){
            $this->error('暂时没有设置验证码');
        }
        $UserService =  new UserService();
        $result =  $UserService->createcode($code);
        $data = [
            'qr_code'=>$result,
            'code'=>$code
        ];
        $this->success('success',$data);
    }
}